package cn.tjnext.apply.web.interceptor;

import cn.tjnext.apply.web.model.LoginModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 检测登陆
 */
public class BackLoginInterceptor implements HandlerInterceptor {

    protected static final Logger log = LoggerFactory.getLogger(BackLoginInterceptor.class);

    // 进入Handler方法之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 验证是否登陆
        HttpSession session = request.getSession();
        LoginModel loginModel = (LoginModel) session.getAttribute("admin");
        if (session.isNew() || loginModel == null) {
            response.sendRedirect(request.getContextPath() + "/back/login");
            return false;
        }
        return true;
    }

    // 进入Handler方法之后，返回modelAndView之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    // 退出Handle方法后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
